package com.example.springbootredis.controller;

import com.example.springbootredis.entity.User;
import com.example.springbootredis.util.BaseResp;
import com.example.springbootredis.util.TestDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Set;


@Slf4j
@Api("redis【set】")
@RestController
@RequestMapping("/redis/set")
public class RedisSetController {

    @Autowired
    RedisTemplate redisTemplate;



    //-----------------redis set start-------------
    String testSetKey = "test_set";
    String testSetKey_1 = "test_set_1";
    String testSetKey_2 = "test_set_2";
    String testSetKey_3 = "test_set_3";

    @ApiOperation("初始化数据")
    @RequestMapping(value = "/initData",method = RequestMethod.GET)
    public BaseResp set_initData(){
        log.info("list_initData start");
        Long resp = redisTemplate.opsForSet().add(testSetKey,TestDataUtil.getUsers().toArray());

        redisTemplate.opsForSet().add(testSetKey_1,"A","B","C");
        redisTemplate.opsForSet().add(testSetKey_2,"A","a","B");
        redisTemplate.opsForSet().add(testSetKey_3,"A");
        log.info("list_initData end resp:{}",resp);
        return BaseResp.buildSuccess();
    }

    @ApiOperation("set-拉取")
    @RequestMapping(value = "/pop",method = RequestMethod.GET)
    public BaseResp<User> set_pop(){
        SetOperations<String, User> setOperations  = redisTemplate.opsForSet();
        User user= setOperations.pop(testSetKey);
        return BaseResp.buildDataSuccess(user);
    }

    @ApiOperation("randomMember 随机返回一个元素")
    @RequestMapping(value = "/randomMember",method = RequestMethod.GET)
    public BaseResp<User> set_randomMember(){
        SetOperations<String, User> setOperations  = redisTemplate.opsForSet();
        User user= setOperations.randomMember(testSetKey);
        return BaseResp.buildDataSuccess(user);
    }

    @ApiOperation("添加一个元素")
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public BaseResp<Long> set_add(@RequestParam("setKey")String setKey,@RequestParam("setVal")String setVal){
        SetOperations<String, String> setOperations  = redisTemplate.opsForSet();
        Long resp = setOperations.add(setKey,setVal);
        return BaseResp.buildDataSuccess(resp);
    }

    @ApiOperation("判断一个元素是否存在")
    @RequestMapping(value = "/isMember",method = RequestMethod.GET)
    public BaseResp<Boolean> set_isMember(@RequestParam("setKey")String setKey,@RequestParam("setVal")String setVal){
        SetOperations<String, User> setOperations  = redisTemplate.opsForSet();
        boolean resp = setOperations.isMember(setKey,setVal);
        return BaseResp.buildDataSuccess(resp);
    }

    @ApiOperation("获取set的元素数量")
    @RequestMapping(value = "/size",method = RequestMethod.GET)
    public BaseResp list_pop1(@RequestParam("setKey")String setKey){
        SetOperations<String, User> setOperations  = redisTemplate.opsForSet();
        return BaseResp.buildDataSuccess(setOperations.size(setKey));
    }



    @ApiOperation("多个set-差集(取第一个set集合中 在其他set集合没有出现的元素)")
    @RequestMapping(value = "/difference",method = RequestMethod.GET)
    public BaseResp list_difference(){
        SetOperations<String, String> setOperations  = redisTemplate.opsForSet();
        Set<String> differenceSet = setOperations.difference(Arrays.asList(testSetKey_1,testSetKey_3,testSetKey_2));
        return BaseResp.buildDataSuccess(differenceSet);
    }

    @ApiOperation("多个set-并集")
    @RequestMapping(value = "/union",method = RequestMethod.GET)
    public BaseResp list_union(){
        SetOperations<String, String> setOperations  = redisTemplate.opsForSet();

        Set<String> differenceSet = setOperations.union(Arrays.asList(testSetKey_1,testSetKey_2,testSetKey_3));
        return BaseResp.buildDataSuccess(differenceSet);
    }
    @ApiOperation("多个set-交集")
    @RequestMapping(value = "/intersect",method = RequestMethod.GET)
    public BaseResp list_intersect(){
        SetOperations<String, String> setOperations  = redisTemplate.opsForSet();
        Set<String> differenceSet = setOperations.intersect(Arrays.asList(testSetKey_1,testSetKey_2,testSetKey_3));
        return BaseResp.buildDataSuccess(differenceSet);
    }

    //-----------------redis set end-------------

}
